<template>
  <div>
    <h1 align="center">emp</h1>
    <hr>
    <!--  查询条件开始-->
    <el-form ref="searchFrm" :model="queryForm" label-width="80px" :inline="true">
      <el-form-item label="所属部门" prop="deptList">
        <el-select v-model="queryForm.params.deptId">
          <el-option :label="item.deptName" v-for="(item) in deptList" :value="item.deptId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工姓名" prop="empName">
        <el-input prefix-icon="请输入姓名" v-model="queryForm.params.empName"></el-input>
      </el-form-item>
      <el-form-item label="员工工资" prop="empSalary">
        <el-input prefix-icon="请输入员工工资" v-model="queryForm.params.empSalary"></el-input>
      </el-form-item>
      <el-form-item label="员工性别" prop="empSex">
        <el-radio-group v-model="queryForm.params.empSex">
          <el-radio label="1">男</el-radio>
          <el-radio label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="doSearch">查询</el-button>
        <el-button type="warning" icon="el-icon-close" @click="doReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!--  查询条件结束-->

    <!--表格工具行开始-->
    <div style="margin: 10px" align="left">
      <el-button type="primary" icon="el-icon-plus" @click="toAdd">新增</el-button>
    </div>
    <!--表格工具行结束-->

    <!--  表格开始-->
    <el-table :data="empList" v-loading="loading">
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column label="编号" align="center" prop="empId"></el-table-column>
      <el-table-column label="姓名" align="center" prop="empName"></el-table-column>
      <el-table-column label="性别" align="center" prop="empSex">
        <template slot-scope="{row}">
          {{ row.empSex == 1 ? "男" : "女" }}
        </template>
      </el-table-column>
      <el-table-column label="工资" align="center" prop="empSalary"></el-table-column>
      <el-table-column label="生日" align="center" prop="empBirth"></el-table-column>
      <el-table-column label="所属部门" align="center" prop="deptId">
        <template slot-scope="{row}">
          <span v-for="item in deptList" v-if="item.deptId == row.deptId">{{ item.deptName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" style="width: 200px">
        <template slot-scope="{row}">
          <el-button type="text" icon="el-icon-edit" @click="toEdit(row)">修改</el-button>
          <el-button type="text" icon="el-icon-delete" @click="toDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        :hide-on-single-page="onlyOnePageVisible"
        :current-page="queryForm.pageNum"
        :page-size="queryForm.pageSize"
        :total="queryForm.total"
        layout="sizes,total, prev, pager, next, jumper"
        :page-sizes="pageSizes"
        @current-change="pageNumChange"
        @size-change="pageSizeChange"
    >
    </el-pagination>
    <!--  表格结束-->

    <!--  添加框开始-->
    <el-dialog :visible.sync="addOrEditWindows" :title=addOrEditTitle width="700px">
      <div>
        <el-row>
          <el-col :span="10">
            <el-form ref="addOrEditForm" :rules="addOrEditFormRule" :model="addOrEditFormData" align="left">
              <el-form-item label="员工姓名" prop="empName" label-width="80px">
                <el-input placeholder="请输入员工姓名" v-model="addOrEditFormData.empName"></el-input>
              </el-form-item>
              <el-form-item label="员工工资" prop="empSalary" label-width="80px">
                <el-input placeholder="请输入员工工资" v-model="addOrEditFormData.empSalary"></el-input>
              </el-form-item>
              <el-form-item label="员工性别" prop="empSex" label-width="80px">
                <el-radio-group v-model="addOrEditFormData.empSex">
                  <el-radio :label="1">男</el-radio>
                  <el-radio :label="0">女</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="员工生日" prop="empBirth" label-width="80px">
                <el-date-picker v-model="addOrEditFormData.empBirth" type="datetime" value-format="yyyy-MM-dd"
                                v></el-date-picker>
              </el-form-item>
              <el-form-item label="员工部门" label-width="80px">
                <el-select v-model="addOrEditFormData.deptId">
                  <el-option :label="item.deptName" v-for="(item) in deptList" :value="item.deptId"></el-option>
                </el-select>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
      <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="doSubmit">确 定</el-button>
      <el-button @click="addOrEditWindows = false">取 消</el-button>
    </span>
    </el-dialog>
    <!--  修改框结束-->
  </div>
</template>

<script>
import deptApi from "@/api/dept";
import empApi from "@/api/emp";

export default {
  name: "empView",
  data() {
    return {
      queryForm: {
        pageNum: 1,
        pageSize: 5,
        total: 0,
        params: {}
      },
      totalPage: 0,
      onlyOnePageVisible: false,
      pageSizes: [5, 10, 20],
      deptList: [],
      empList: [],
      //新增/编辑窗口是否开启
      addOrEditWindows: false,
      //修改窗口是否开启
      addOrEditTitle: undefined,
      addOrEditFormData: {
        empName: undefined,
        empAddress: undefined,
        empSex: undefined,
        deptId: undefined,
        empSalary: undefined,
        empBirth: undefined
      },
      addOrEditFormRule: {
        empName: [
          {required: true, message: '请输入员工姓名', trigger: 'blur'},
          {min: 2, max: 10, message: '姓名长度在2到10个字符', trigger: 'blur'}
        ],
        empSalary: [
          {required: true, message: '请输入工资', trigger: 'blur'},
        ],
        empSex: [
          {required: true, message: '请选择性别', trigger: 'change'}
        ]
      },
      loading: false
    }
  },
  methods: {
    doSubmit: function () {
      //表单验证
      this.$refs['addOrEditForm'].validate((valid) => {
        if (valid) {
          //表单验证通过，执行提交操作
          if (this.addOrEditFormData.empId) {
            //编辑操作
            empApi.updateEmp(this.addOrEditFormData).then(res => {
              this.addOrEditWindows = false;
              this.loadAllEmp();
              this.$notify({
                title: '成功',
                message: '员工信息修改成功',
                type: 'success'
              });

              this.loadAllEmp();
            }).catch(err => {
              this.$notify({
                title: '修改失败',
                message: err.data.msg,
                type: 'error'
              });
            })
          } else {
            //新增操作
            empApi.addEmp(this.addOrEditFormData).then(res => {
              this.addOrEditWindows = false;
              this.loadAllEmp();
              this.$notify({
                title: '成功',
                message: '员工信息添加成功',
                type: 'success'
              });
              this.loadAllEmp();
            }).catch(err => {
              this.$notify({
                title: '添加失败',
                message: err.data.msg,
                type: 'error'
              });
            })
          }
        }
      })
      this.addOrEditWindows = false;

    },
    //分页查询所有员工信息
    loadAllEmp: function () {
      this.loading = true;
      empApi.getAllEmpByPage(this.queryForm).then(res => {
        this.empList = res.data.data;
        this.loading = false;
        this.queryForm.total = res.data.total;
        this.totalPage = res.data.pages;
      }).catch(err => {
        this.loading = false;
      })
    },
    pageSizeChange(val) {
      this.queryForm.pageSize = val;
      this.loadAllEmp();
    },
    //
    pageNumChange(val) {
      this.queryForm.pageNum = val;
      this.loadAllEmp();
    },
    toEdit(row) {
      this.addOrEditFormData = {}
      this.addOrEditWindows = true;
      this.addOrEditTitle = "修改【" + row.empName + "】的员工信息",
          this.addOrEditFormData = row
    },
    toDelete(row) {
      this.$confirm("请二次确实是否删除", "温馨提示", {
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      }).then(() => {
        console.log(row);
        empApi.deleteEmp(row.empId).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功',
          })
          if (this.queryForm.pageNum != 1 && this.empList == 1) {
            this.queryForm.pageNum -= 1;
          }
          this.loadAllEmp();
        }).catch(() => {

        })
      })
    },
    //添加
    toAdd() {
      this.addOrEditWindows = true;
      this.addOrEditTitle = "新增员工",
          this.addOrEditFormData = {}
    },
    //重置
    doReset() {
      this.queryForm.params = {};
    },
    //条件查询
    doSearch: function () {
      this.queryForm.pageNum = 1;
      this.loadAllEmp();
    },
    //查询所有部门
    loadAllDept: function () {
      deptApi.getAllDept().then(res => {
        this.deptList = res.data;
      })
    }
  },
  created() {
    this.loadAllDept();
    this.loadAllEmp();
  }
}
</script>

<style scoped>

</style>